package main

import (
	"fmt"
	"time"
)

func write(ch chan int) {
	for i := 0; i < 5; i++ {
		ch <- i
		fmt.Println("success wrote", i, "to channel")
	}
	close(ch)
}

func main() {

	//demo1 deadlock 没有其他的协程从 ch 接收数据。于是程序触发 panic
	// ch := make(chan int)
	// ch <- 5

	//demo2
	ch := make(chan int, 2)
	go write(ch)
	time.Sleep(2 * time.Second)

	for v := range ch {
		fmt.Println("read val", v, "from channel")
		time.Sleep(2 * time.Second)

	}
}
